<?php

function list_rekomendasi_lembur($periode_id=0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}	
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&task=list_rekomendasi_lembur&periode_id=\'+periode_id ;
		}
	');
	if($periode_id==0){
		$periode_id=get_periode_gaji() ;
	}	
	$query_check_periode = " SELECT * FROM pr_periode ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('m-Y',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$src ="<form method=\"GET\">
	<input type=\"hidden\" name=\"com\" value=\"{$_GET['com']}\" />
	<table border=\"0\" width=\"100%\"> <tr><td width='10%'>
	Periode&nbsp;:</td><td width='10%'>".form_dropdown($fperiode_id,$prs)." </td>
	</tr></table></form>";
	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nomor Surat'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Tanggal Surat'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Nama'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	); 
	
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$startdate = $periode['dari'];	
	$enddate  = $periode['sampai'];
	$query = "SELECT * FROM a_rekomendasi_lembur a
		WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)  
		AND a.periode_id={$periode_id} 
		ORDER BY rekomendasi_lembur_id DESC
	";
 
	$result = my_query($query );
 
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&periode_id={$_GET['periode_id']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['rekomendasi_lembur_id'].'&periode_id='.$periode_id,
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['rekomendasi_lembur_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'nomor'=>  strtoupper($ey['nomor_surat'] ),  
			'tanggal'=>  position_text_align( date('d-m-Y',strtotime($ey['tanggal_surat'] )),'center'),  
			'nik'=>  strtoupper($karyawan['nik'] ),  
			'nama'=>  strtoupper($karyawan['nama_gelar'] ),  
			'operasi'=> position_text_align(  $edit_button .' '. $delete_button  , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);  
	$menuontop = array(  
		'Lembur 56jam'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=daftar_lembur&periode_id='.$periode_id.'\';'),
		'Tambah Data'=>array(
			'onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=edit&periode_id='.$periode_id.'\' });'
		)
	);
	$printmenu = additional_menu_on_list( $menuontop ,$src);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas , 8, false,$paging );
} 


function form_create_rekomendasi_lembur( $id=0, $periode_id ){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);
	my_set_file_css(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);
	
	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0];
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');
	
	$view = form_header( "seleski" , "seleski"  );
 	$fields = my_get_data_by_id('a_rekomendasi_lembur','rekomendasi_lembur_id',$id);
	$karyawan = loaddata_karyawan( $fields['karyawan_id']);
	
	$form_nomor_surat = array(
		'name'=>'nomor_surat',
		'id'=>'nomor_surat',
		'readonly'=>'readonly',
		'value'=>isset($_POST['nomor_surat']) ?$_POST['nomor_surat'] : ($fields ? $fields['nomor_surat']: getNumber(date('Y'),date('m') )),
	);
	$fields_form_nomor = form_dynamic($form_nomor_surat);
	$view  .= form_field_display( '<span class="label_form">'.$fields_form_nomor.'</span>'   , "<b>Nomor Surat</b>"    ); 
	
	$ftanggal = date('d-m-Y');
	if($fields){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$fields['tanggal_surat'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal; 
	}
	
	$tanggal_surat = array(
			'name'=>'tanggal_surat',
			'value'=>(isset($_POST['tanggal_surat'])? $_POST['tanggal_surat'] : $ftanggal),
			'id'=>'tanggal_surat',
			'type'=>'textfield',
			'size'=>'45'
		);
	$form_tanggal = form_calendar($tanggal_surat);
	$view .= form_field_display( $form_tanggal  , "<b>Tanggal Surat</b>" );
	
	$nama_karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'] ) ? $_POST['nama_karyawan'] : ($fields? $karyawan['nik'].'/'.$karyawan['nama'] :'') ),
		'id'=>'nama_karyawan' , 
		'type'=>'text',
		'size'=>'45' ,
		'onchange'=>'javascript:checkName(this.value)'
	);
	$form_field_nama_karyawan = form_dynamic($nama_karyawan);
	$view .= form_field_display( $form_field_nama_karyawan , "<b>NIK / Nama karyawan</b>"    ); 

	if($periode_id==0){
		$periode_id=get_periode_gaji() ;
	}	
	$query_check_periode = " SELECT * FROM pr_periode ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('Y-m',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>(isset($_POST['periode_id'])? $_POST['periode_id'] : $periode_id),
		'name'=>'periode_id',
	);
	$form_periode = form_dropdown($fperiode_id , $prs);
	$view .= form_field_display( $form_periode , "<b>Periode</b>" );

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
/*	
	$back = array(
		'value' => ( 'Kembali'),
		'name' => 'simpan', 
		'type'=>'button',
		'onclick'=>'javascript:location.href=(\'index.php?com=a_rekomendasi_lembur\')',
		'class'=>'main_button'
	);
	$form_back= form_dynamic($back); 
	$view .= form_field_display( $form_submit .' '.$form_back , "&nbsp;" ,  "" );
*/
	$view .= form_field_display( $form_submit , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}
 

function form_rekomendasi_lembur_submit($id){ 
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal_surat'] );
	$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
				
	$datas = array(
		'nomor_surat'	=>my_type_data_str($_POST['nomor_surat']),
		'tanggal_surat'	=>my_type_data_str($ftanggal),
		'karyawan_id'	=>my_type_data_str($karyawan_id),
		'periode_id'	=>my_type_data_int($_POST['periode_id']),
	);
	if($id > 0){
		return my_update_record('a_rekomendasi_lembur' , 'rekomendasi_lembur_id', $id ,$datas);
	}
	return my_insert_record('a_rekomendasi_lembur' , $datas);
}



function rekomendasi_lembur_validate( $id ){
	$errsubmit = false;
	$err = array();
	 
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
	$ftanggal	 = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
	/*CHECK NOMOR SURAT*/
	if($karyawan_id == 0){
		$errsubmit = true;
		$err[] = "Nama karyawan tidak di temukan";	
	}
	
	if(!$ftanggal){
		$errsubmit = true;
		$err[] = "Tanggal tidak benar";	
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function getNumber($tahun,$bulan){
	$query = "SELECT max(nomor_surat) as nn FROM a_rekomendasi_lembur 
		WHERE nomor_surat LIKE '%{$tahun}{$bulan}%' ";
	$res = my_query($query);
	$row = my_fetch_array($res); 
 
	return $tahun.$bulan.sprintf('%05d',(int)substr($row['nn'] ,6,12) + 1);
}

function list_daftar_lembur($periode_id=0){
	my_set_code_js('
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&task=list_daftar_lembur&periode_id=\'+periode_id ;
		}
	');


	if($periode_id==0){
		$periode_id=get_periode_absen() ;
	}	

		$query_check_periode = " SELECT * FROM pr_periode ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('m-Y',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$src ="<form method=\"GET\">
		<input type=\"hidden\" name=\"com\" value=\"{$_GET['com']}\" />
		<table border=\"0\" width=\"100%\"> <tr><td width='10%'>
		Periode&nbsp;:</td><td width='10%'>".form_dropdown($fperiode_id,$prs)." </td>
		</tr></table></form>";
	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Nama'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Jam Lembur'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	); 
		$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
		$startdate = $periode['dari'];	
		$enddate  = $periode['sampai'];
		$query = "SELECT karyawan_id, sum(hour) as jml_lembur FROM temp_tmn_overtime_new
		WHERE ( DATE(tanggal) >= '{$startdate}' AND DATE(tanggal) <='{$enddate}' )	 
		GROUP BY karyawan_id 
		ORDER BY karyawan_id 
	"; 

	$result = my_query($query );
 	$row = array(); 
	$i = 0; 
	while($ey = my_fetch_array($result)){
		if($ey['jml_lembur']>56){
			$i++;
			$karyawan =  loaddata_karyawan($ey['karyawan_id']); 
			$jabatan =  loaddata_jabatan($karyawan['jabatan_id']);  
			
			$query = "SELECT karyawan_id FROM a_rekomendasi_lembur
				WHERE karyawan_id = {$ey['karyawan_id']} 
				AND periode_id={$periode_id} "; 
			$dres= my_query($query);
			if(my_num_rows($dres)>0){
				$create_button=' ';
			}else{
				$createproperty = array(
					'href'=>'index.php?com='.$_GET['com'].'&task=auto_create&id='.$ey['karyawan_id'].'&periode_id='.$periode_id,
					'rel'=>'facebox',
					'title'=>'Create'
				);
				$create_button = button_icon( 'b_bookmark.png' , $createproperty  );	
			}	

			$row[] = array(
				'#'=>position_text_align ($i, 'center'),
				'nik'=> $karyawan['nik'],
				'nama'=> $karyawan['nama_gelar'],  
				'jabatan'=> $jabatan['nama_jabatan'],  
				'jumlah lembur'=> position_text_align($ey['jml_lembur'], 'right'),
				'operasi'=> position_text_align( $create_button, 'right'),  				
			);
		}			 
	}
	
	$datas = table_rows($row); 
	$menuontop = array(  
		'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'\''), 
	);	
	
	$printmenu = additional_menu_on_list( $menuontop ,$src);
	return $printmenu .table_builder($header , $datas ,  8, false,'' );
} 


function form_auto_create( $id, $periode_id ){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);
	
	$view = form_header( "seleski" , "seleski"  );
	$karyawan = loaddata_karyawan( $id );
	
	$form_nomor_surat = array(
		'name'=>'nomor_surat',
		'id'=>'nomor_surat',
		'readonly'=>'readonly',
		'value'=>isset($_POST['nomor_surat']) ?$_POST['nomor_surat'] : getNumber(date('Y'),date('m') ),
	);
	$fields_form_nomor = form_dynamic($form_nomor_surat);
	$view  .= form_field_display( '<span class="label_form">'.$fields_form_nomor.'</span>'   , "<b>Nomor Surat</b>"    ); 
	
	$ftanggal = date('d-m-Y');
	$tanggal_surat = array(
			'name'=>'tanggal_surat',
			'value'=>(isset($_POST['tanggal_surat'])? $_POST['tanggal_surat'] : $ftanggal),
			'id'=>'tanggal_surat',
			'type'=>'textfield',
			'size'=>'45'
		);
	$form_tanggal = form_calendar($tanggal_surat);
	$view .= form_field_display( $form_tanggal  , "<b>Tanggal Surat</b>" );
	
	$view .= form_field_display( $karyawan['nik'].'/'.$karyawan['nama'] , "<b>NIK / Nama  </b>"    ); 
	$periodes=my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$view .= form_field_display( date('d-m-Y',strtotime($periodes['dari'])) .' s/d '.date('d-m-Y',strtotime($periodes['sampai'])) , "<b>Periode</b>" );

	$submit = array(
		'value' => ' Simpan ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit , "&nbsp;" ,  "" );

	$view .= form_footer( );
	return $view;
}
 

function form_auto_create_submit($id,$periode_id ){
	list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal_surat'] );
	$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
				
	$datas = array(
		'nomor_surat'	=>my_type_data_str($_POST['nomor_surat']),
		'tanggal_surat'	=>my_type_data_str($ftanggal),
		'karyawan_id'	=>my_type_data_str($id),
		'periode_id'	=>my_type_data_int($periode_id),
	);
	return my_insert_record('a_rekomendasi_lembur' , $datas);
}



function auto_create_validate( $id,$periode_id ){
	$errsubmit = false;
	$err = array();
	 
	list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
	$ftanggal	 = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
	/*CHECK NOMOR SURAT*/
	if($id == 0){
		$errsubmit = true;
		$err[] = "Nama karyawan tidak di temukan";	
	}
	
	if(!$ftanggal){
		$errsubmit = true;
		$err[] = "Tanggal tidak benar";	
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}